/*******************************************************************************
*
*   FILE NAME:
*       String.cpp - String class implementation
*
*   DESCRIPTION:
*       Represents text as a series of Unicode characters.
*
*******************************************************************************/
#include "String.h"

#include <string.h>

String::String( void )
{
    mData = new tchar[ 1 ];
    mLength = 0;
    mData[ mLength ] = NULL;
}

String::String( const tchar* string_data )
{
    if( string_data == NULL )
    {
        mData = new tchar[ 1 ];
        mLength = 0;
        mData[ mLength ] = NULL;
        return;
    }

    for( mLength = 0; string_data[ mLength ] != NULL; ++mLength );
    mData = new tchar[ mLength + 1 ];
    memcpy( mData, string_data, ( sizeof( tchar ) * mLength ) );
    mData[ mLength ] = NULL;
}

String::String( const String& other )
{
    mLength = other.mLength;
    mData = new tchar[ mLength + 1 ];
    memcpy( mData, other, ( sizeof( tchar ) * mLength ) );
    mData[ mLength ] = NULL;
}

String::~String( void )
{
    if( mData != NULL )
    {
        delete[] mData;
    }
}

String&  String::operator=( const tchar* string_data )
{
    if( mData != NULL )
    {
        delete[] mData;
    }

    if( string_data == NULL )
    {
        mData = new tchar[ 1 ];
        mLength = 0;
        mData[ mLength ] = NULL;

        return *this;
    }

    for( mLength = 0; string_data[ mLength ] != NULL; ++mLength );
    mData = new tchar[ mLength + 1 ];
    memcpy( mData, string_data, ( sizeof( tchar ) * mLength ) );
    mData[ mLength ] = NULL;

    return *this;
}

String& String::operator=( const String& other )
{
    if( mData != NULL )
    {
        delete[] mData;
    }

    mLength = other.mLength;
    mData = new tchar[ mLength + 1 ];
    memcpy( mData, other, ( sizeof( tchar ) * mLength ) );
    mData[ mLength ] = NULL;

    return *this;
}

String& String::operator+=( const String& other )
{
    tchar* tmp = mData;
    mData = new tchar[ mLength + other.mLength + 1 ];

    memcpy( mData, tmp, ( sizeof( tchar ) ) * mLength );
    memcpy( &( mData[ mLength ] ), other.mData, ( sizeof( tchar ) ) * other.mLength );
    mLength += other.mLength;
    mData[ mLength ] = NULL;

    delete[] tmp;

    return *this;
}

boolean operator==( const String& str1, const String& str2 )
{
    return ( memcmp( str1, str2, str1.mLength) == 0 );
}

boolean operator!=( const String& str1, const String& str2 )
{
    return ( memcmp( str1, str2, str1.mLength) != 0 );
}

String operator+( const String& str1, const String& str2 )
{
    String ret;

    ret.mLength = str1.mLength + str2.mLength;
    ret.mData = new tchar[ ret.mLength + 1 ];
    
    memcpy( ret.mData, str1.mData, ( sizeof( tchar ) ) * str1.mLength );
    memcpy( &( ret.mData[ str1.mLength ] ), str2.mData, ( sizeof( tchar ) ) * str2.mLength );
    ret.mData[ ret.mLength ] = NULL;

    return ret;
}

String::operator tchar*() const
{
    return mData;
}

uint32 String::GetLength( void )
{
    return mLength;
}
